<template>
  <div class="login">
    <div class="login_content">
      <h1>欢迎来到小赵的后台</h1>
      <el-form ref="form" :model="form" :rules="rules" validate>
        <el-form-item prop='user'>
          <el-input v-model="form.username" placeholder="请输入用户名">
            <i slot="prefix" class="el-input__icon el-icon-s-custom"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input v-model="form.password" placeholder="请输入密码" type="password" :show-password="true">
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')" style="width:100%;">
            确定
          </el-button>
        </el-form-item>
        <el-form-item class="textRight">
          <el-tag @click="callMy" style="cursor: pointer;">联系我</el-tag>
        </el-form-item>
      </el-form>
    </div>
    <Vcode :show="VcoedIsShow" @success="success" />
  </div>
</template>
<script>
// 导入人机验证模块
import Vcode from 'vue-puzzle-vcode'
import { mapMutations } from 'vuex'
export default {
  components: {
    Vcode
  },
  data () {
    const validatePass = (rule, value, callback) => {
      if (!this.form.password.trim()) {
        callback(new Error('请输入密码'))
      } else {
        const reg = /^[\w+$]{6,12}$/
        reg.test(this.form.password) || callback(new Error('密码只能是6-12位的数字下划线和字母'))
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (!this.form.username.trim()) {
        callback(new Error('请输入用户名'))
      } else {
        const reg = /^.{2,8}$/
        reg.test(this.form.username) || callback(new Error('用户名长度是2-8位'))
        callback()
      }
    }
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        user: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      VcoedIsShow: false
    }
  },
  methods: {
    ...mapMutations(['ChangeUsername', 'Changeavatar']),
    // 去向后端去查找有没有user
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 去请求数据
          this.$axios.post('/login/user', {
            username: this.form.username,
            password: this.form.password
          }).then(res => {
            if (res.data.ok) {
              // 给全局方法存储username avatar
              this.ChangeUsername(res.data.username)
              this.Changeavatar(res.data.avatar)
              this.VcoedIsShow = true
            } else {
              this.callMy()
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 人机验证成功
    success () {
      // 就跳转页面
      this.VcoedIsShow = false

      this.$router.push('/home')
    },
    callMy () {
      this.$alert('登录不上去加我微信  18803425419 ', '微信', {
        confirmButtonText: '确定',
        callback: action => {
          this.$message({
            type: 'info',
            message: '等待消息吧'
          })
        }
      })
    }
  }
}
</script>

<style>
.el-input .el-input__clear {
  font-size: 16px;
}
</style>
<style lang="scss" scoped>
$bgTeme: #283443;
$colorTeme: #eee;
.login {
  position: absolute;
  width: 100%;
  min-height: 100%;
  background: #2d3a4b;
  background-size: cover;
  color: #fff;
}
.login_content {
  margin: 160px auto;
  max-width: 300px;
  h1 {
    font-size: 25px;
    font-weight: 700;
    margin-bottom: 30px;
    text-align: center;
  }
  .textRight {
    text-align: right;
  }
}
</style>
